import {
	defineStore
} from 'pinia'
import {
	fetchPlayDetailApi
} from '../../service/api'
import {
	iscode
} from '../../utils/iscode'
import useCommonStore from '../common/common'


const usePlayerStore = defineStore('player', {
	state: () => ({
		playerData: {}
	}),
	actions: {
		// - 获取目标视频的所有集数数据（需要自己分页）
		async fetchPlayerDataAction(skit_id, cb) {
			const res = await fetchPlayDetailApi(skit_id)
			if (iscode(res)) {
				this.playerData = res.result

				const {
					cover_image,
					script_name
				} = res.result

				const commonStore = useCommonStore()
				const findItem = commonStore.followHistory.find(it => it.skit_id == skit_id)

				for (const it of this.playerData.detail) {
					if (findItem) {
						it.isFollow = 1
					} else {

						it.isFollow = 0
					}

					// 后端的数据格式不太方便，个别参数自己添加一下
					it.cover_image = cover_image
					it.script_name = script_name
				}
				cb && cb()
			}
		}
	}
})

export default usePlayerStore